<template>
   <MetaItemHeader :label="t('节点负责人')" :required="true" tips="最多选择100人"></MetaItemHeader> 
  <selected-tags
    v-model="selectedCandidateTags"
    :editable="true"
    :empty-text="'选择成员或部门'"
    @editTag="editTag"
  />
  <member-select-dialog v-model="showMemberDialog" @ok="finishSelect" />
</template>
<script lang="ts" setup>
import {
  FlowNodeType,
  IFlowContext,
  IFlowNodeData,
  CandidateType,
  IApprovalCandidate,
  IFlowNodeMetaData,
  createFlowNode,
} from "../FlowData";
import { ISelectedTag, TagType } from "@eimsnext/components";
import { useLocale } from "element-plus";
import { convertCandidateToTag, convertTagToCandidate } from "./type";
import MetaItemHeader from "../components/MetaItemHeader/index.vue";
const { t } = useLocale();

defineOptions({
  name: "CopyNodeMeta",
});

const flowContext = inject<IFlowContext>("flowContext");
const flowContextRef = reactive<IFlowContext>(flowContext!);
const activeData = ref<IFlowNodeData>(createFlowNode(FlowNodeType.None));
const showMemberDialog = ref(false);
const selectedCandidateTags = ref<ISelectedTag[]>([]);

const editTag = () => {
  showMemberDialog.value = true;
};
const finishSelect = (tags: ISelectedTag[]) => {
  //   console.log("sel tags", tags);
  selectedCandidateTags.value = tags;
  let candidate: IApprovalCandidate[] = [];
  tags.forEach((x) => candidate.push(convertTagToCandidate(x)));
  activeData.value.metadata.copytoMeta!.approvalCandidates = candidate;

  showMemberDialog.value = false;
};

watch(
  flowContextRef,
  (newValue: IFlowContext) => {
    // console.log("activeData", newValue.activeData.metadata);
    activeData.value = newValue.activeData;

    selectedCandidateTags.value = [];
    if (activeData.value.metadata.copytoMeta!.approvalCandidates) {
      let tags: ISelectedTag[] = [];
      activeData.value.metadata.copytoMeta!.approvalCandidates.forEach((x: IApprovalCandidate) =>
        tags.push(convertCandidateToTag(x))
      );
      selectedCandidateTags.value = tags;
    }
  },
  { immediate: true }
);
</script>
